﻿Imports FinanceiroBLL
Imports FinanceiroDAL

Public Class PlanoContasFRM
    Public Op As String
    Public codigo As Integer
    Dim objBLL As New PlanoContasBLL
    Private Sub PlanodeContasFRM_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        objconexao.carregaCombo(CBCentroCusto, "Financeiro.Gen_SubGrupo", "sgr_codigo", "sgr_nome", "", Nothing)
        objconexao.carregaCombo(CBStatus, "Generico.Gen_Status", "sta_codigo", "sta_descricao", "sta_codigo < 3", "sta_codigo")
        CBCentroCusto.SelectedValue = 1
        CBStatus.SelectedValue = 1

        limpar()
        If (Op = "I") Then
            LBCodigo.Visible = False
            LPlanodeContas.Text = "Plano de Contas"
            LBCodigo.Text = 0
        Else
            LBCodigo.Visible = True
            CarregaCampos()
        End If
    End Sub
#Region "Carrega Campos" 'veraqui
    Sub CarregaCampos()
        objBLL = New PlanoContasBLL(codigo)
        LBCodigo.Text = objBLL.Codigo
        ' LPlanodeContas.Text = objBLL.Descricao
        TDescricao.Text = objBLL.Descricao
        CBCentroCusto.SelectedValue = objBLL.GrupoCodigo
        TBGrupo.Text = objBLL.tipo
        CBStatus.SelectedValue = objBLL.Status
    End Sub
#End Region
    Private Sub TSBSalvar_Click(sender As System.Object, e As System.EventArgs) Handles TSBSalvar.Click
        Try
            'codigo, StrConv(Trim(TXTDescricao.Text), VbStrConv.ProperCase), CBStatus.SelectedValue
            objBLL = New PlanoContasBLL(codigo, StrConv(Trim(TDescricao.Text), VbStrConv.ProperCase), CBCentroCusto.SelectedValue, TBGrupo.Text, CBStatus.SelectedValue)

            If Op = "I" Then
                objBLL.Inserir()
                MessageBox.Show("Conta do Plano de Contas inserido com sucesso", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information)
                limpar()
                Me.Close()
            Else
                If (objBLL.Alterar()) Then
                    MessageBox.Show("Conta do Plano de Contas alterada com sucesso", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    limpar()
                    Me.Close()
                Else : MessageBox.Show("A Conta selecionada não pôde ser alterada", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Information)

        End Try
    End Sub
#Region "Limpar campos"
    Sub limpar()
        CBStatus.SelectedValue = 1
        CBCentroCusto.SelectedValue = 1
        TDescricao.Text = ""
        Me.objBLL = Nothing
    End Sub
#End Region

    Private Sub CBCentroCusto_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles CBCentroCusto.SelectedIndexChanged
        If (Val(CBCentroCusto.SelectedValue.ToString) > 0) Then
            Dim objcentrocusto As New CentroCustoBLL(CBCentroCusto.SelectedValue)
            'TBGrupo.Text = 
            If (objcentrocusto.codgrupo = 2) Then
                TBGrupo.Text = "Saída"
            Else
                TBGrupo.Text = "Entrada"
            End If
        End If
    End Sub

    Private Sub TSBVoltar_Click(sender As System.Object, e As System.EventArgs) Handles TSBVoltar.Click
        Close()
        objBLL = Nothing
    End Sub


End Class